const express = require('express');
const merchantController = require('../controllers/merchantController');
const authMiddleware = require('../../../middleware/adminauthMiddleware');
const userController = require('../controllers/userController');
const productController = require('../controllers/productController');
const categoryController = require('../controllers/categoryController');
const orderController = require('../controllers/orderController');
const path = require('path');
const multer = require('multer');

const router = express.Router();
// 配置 Multer
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'public/uploads/'); // 存储位置
    },
    filename: (req, file, cb) => {
        const ext = path.extname(file.originalname);
        cb(null, Date.now() + ext); // 文件名
    }
});

// 获取用户信息接口
router.get('/merchant/info', authMiddleware, merchantController.getMerchant);
router.post('/login', merchantController.login);
router.get('/user/getList', userController.getList);
router.post('/user/createOrUpdate', userController.createOrUpdate);
router.get('/user/getDetail/:id', userController.getDetail);
router.delete('/user/delete/:id', userController.deleteUser);
// 商品列表管理接口
router.get('/product/getList', productController.getList);
router.post('/product/createOrUpdate', productController.createOrUpdate);
router.get('/product/getDetail/:id', productController.getDetail);
router.delete('/product/delete/:id', productController.deleteProduct);
// 商品分类管理接口
router.get('/category/getList', categoryController.getList);
router.post('/category/createOrUpdate', categoryController.createOrUpdate);
router.get('/category/getDetail/:id', categoryController.getDetail);
router.delete('/category/delete/:id', categoryController.deleteCategory);

// 图片上传接口
const upload = multer({ storage: storage });
router.post('/upload', upload.single('file'), (req, res) => {
    if (!req.file) {
        return res.status(400).json({ message: '请上传图片' });
    }
    const imageUrl = `http://localhost:8888/uploads/${req.file.filename}`; // 图片URL
    res.status(200).json({ message: '上传成功', imageUrl: imageUrl });
});
// 多图片上传接口
router.post('/uploads', upload.array('files', 10), (req, res) => {
    if (!req.files || req.files.length === 0) {
        return res.status(400).json({ message: '请上传图片' });
    }
    const imageUrls = req.files.map(file => `http://localhost:8888/uploads/${file.filename}`);
    res.status(200).json({ message: '上传成功', imageUrls: imageUrls });
});
module.exports = router;

//订单管理接口
router.get('/order/getOrderList', orderController.getOrderList);